package capricornus.components.text
{
	import capricornus.components.label.Label;
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.components.tilelist.TileList;
	import capricornus.components.utils.FilterUtils;
	
	import flash.display.LineScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	public class TextArea extends AbstractComponent
	{
		private var _textField:TextField;
		
		private var _list:TileList;
		
		public function TextArea()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_list = new TileList(1);
			_container.addChild( _list );
			
			_textField = new TextField();
			_textField.defaultTextFormat = Label.DEFAULT_TEXT_FORMAT;
			_textField.textColor = Label.DEFAULT_TEXT_COLOR;
			_textField.multiline = true;
			_textField.wordWrap = true;
			_textField.filters = FilterUtils.getBlackGlowFilter();
			_list.addItem( _textField );
			
			editabled = false;
			setSize( 100, 100 );
			_textField.addEventListener( Event.CHANGE, onChange, false, 0, true );
		}
		
		override public function setSize(w:int, h:int):void
		{
			super.setSize( w, h );
			
			_textField.width = w;
			_list.setSize( w, h );
			
			_baseBack.scaleX = _baseBack.scaleY = 1;
			_baseBack.graphics.clear();
			_baseBack.graphics.beginFill(0xff0000,0);
			_baseBack.graphics.lineStyle( 1, 0, 1, true, LineScaleMode.NONE );
			_baseBack.graphics.drawRect( 0, 0, w, h );
			_baseBack.graphics.endFill();
		}
		
		private function onChange( e:Event ):void
		{
			refresh();
		}
		
		public function set editabled( value:Boolean ):void
		{
			enabled = value;
			if( value )
			{
				_textField.type = TextFieldType.INPUT;
			}
			else
			{
				_textField.type = TextFieldType.DYNAMIC;
			}
		}
		
		public function set textColor( value:uint ):void
		{
			_textField.textColor = value;
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set text( value:String ):void
		{
			_textField.text = value;
			refresh();
		}
		
		public function get htmlText():String
		{
			return _textField.htmlText;
		}
		
		public function set htmlText( value:String ):void
		{
			_textField.htmlText = value;
			refresh();
		}
		
		private function refresh():void
		{
			if( _list.height < _textField.textHeight + 4 )
			{
				_textField.width = _list.width - _list.scrollBarWidth;
				_textField.height = _textField.textHeight + 4;
				_list.refresh();
			}
			else
			{
				_textField.width = _list.width;
				_textField.height = _textField.textHeight + 4;
				_list.refresh();
			}
			_list.proportion = 1;
		}
	}
}
















